import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { Message } from 'element-ui';

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/',
    redirect:'/login'//访问/时重定向到登录页面
  },
  {
    path: '/home', 
    name: 'home',
    component: () => import('../views/HomeView.vue'),
    children:[
      {
        path: 'business-list',
        name: 'business-list',
        component: () => import('../views/business/BusinessList.vue'),
      },
      {
        path: 'business-add',
        name: 'business-add',
        component: () => import('../views/business/BusinessAdd.vue'),
      },
      {
        path: 'business-updata/:b_id',
        name: 'business-updata',
        component: () => import('../views/business/BusinessUpdata.vue'),
        props:true
      },
      {
        path: 'user-list',
        name: 'user-list',
        component: () => import('../views/user/UserList.vue'),
      },
      {
        path: 'user-update/:u_id',
        name: 'user-update',
        component: () => import('../views/user/UserUpdate.vue'),
        props:true
      },
      {
        path: 'order-list',
        name: 'order-list',
        component: () => import('../views/order/OrderList.vue'),
      },
      
      {
        path: 'dairy-list',
        name: 'dairy-list',
        component: () => import('../views/dairy/DairyList.vue'),
      },
      {
        path: 'dairy-add',
        name: 'dairy-add',
        component: () => import('../views/dairy/DairyAdd.vue'),
      },
    ]
  },
  {
    path: '/about',
    name: 'about',
    component: () => import( '../views/AboutView.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


// 前置路由守卫
router.beforeEach((to, from, next) => {

  if (to.path === '/login') {
    next();
  } else {
    let token = sessionStorage.getItem('token');

    if (token === null || token === '') {

    alert('请先登录!')

      next('/login');
    } else {
      next();
    }
  }


})

export default router
